﻿/*----------------------------------------------------------------
    Copyright (C) 2015 Senparc
    
    文件名：HomeController.cs
    文件功能描述：首页Controller
    
    
    创建标识：Senparc - 20150312
----------------------------------------------------------------*/

using ITXHWEB.Controllers;
using System;
using System.Collections.Generic;
//using System.Text.RegularExpressions;
using System.Web.Mvc;
namespace Senparc.Weixin.MP.Sample.Controllers
{
    public class HomeController:ITXController
    {

        public HomeController()
        {
            MAPP = "Home";
            ViewBag.IP = UserIP;
            if (!ViewBag.IsLogOn)
            {
                LogOn();
                GetToken();
                
                return;
            }

            IsVVTReady = false;
            IsVKPReady = false;
            VKPAUF = "";
            LIEFSCH = "";
            WBLIST = null;
            CPWBS = "";
            TWBS = 0;
            HWBS = 0;

            foreach (KeyValuePair<string, object> kp in ENVMAIN)
            {
                TempData[kp.Key] = kp.Value;
            }
            //Tracer.Info("Home","回到主页");
            //app.m_log_out.Info("回到主页");
        }
        public ActionResult Index()
        {
            ViewBag.Message = "主页";
            ViewBag.IsLogOn = IsLogOn(UserIP);
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "关于Intex";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "建议";
            return View();
        }
        public ActionResult GetToken()
        {
            try
            {
                APPID = "wx5cf7f7b8480802f1";
                APPSECRET = "86016607dcb9e458eaa8b3c18cb1bfe5";

                //if (!AccessTokenContainer.CheckRegistered(appId))
                //{
                //    AccessTokenContainer.Register(appId, appSecret);
                //}
                var result = CommonAPIs.CommonApi.GetToken(APPID, APPSECRET);//AccessTokenContainer.GetTokenResult(appId);
                ACCESSTOKEN = result.access_token;
             
                //也可以直接一步到位：
                //var result = AccessTokenContainer.TryGetToken(appId, appSecret);
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                //TODO:为简化代码，这里不处理异常（如Token过期）
                return Json(new { error = "执行过程发生错误！" }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}
